package silentDonkey.web.servlet;

import silentDonkey.domain.User;
import silentDonkey.service.UserService;
import silentDonkey.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.獲取要修改的用戶id
        String id = request.getParameter("id");
        // 2.調用Service查詢
        UserService service = new UserServiceImpl();
        User user = service.findUserById(id);

        // 3.將user存入request域中
        request.setAttribute("user", user);
        // 4.轉發請求到update.jsp中
        request.getRequestDispatcher("/update.jsp").forward(request, response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
